package com.catazy.wanandroid.study.main

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.catazy.wanandroid.common_base.ext.Resource
import com.catazy.wanandroid.common_base.ext.logE
import com.catazy.wanandroid.common_service.login.LoginOutService
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

/**
 *  author: wangyb
 *  date: 3/29/21 9:31 PM
 *  description: todo
 */
class MainViewModel : ViewModel() {

    companion object{
        private const val TAG = "MainViewModel"
    }

    val logOutLiveData = MutableLiveData<Resource<String>>()

    /**
     * 退出登录
     * @param loginOutService LoginOutService 登出服务
     */
    fun logOut(loginOutService: LoginOutService) {
        viewModelScope.launch {
            loginOutService.logOut().catch {
                logE(TAG, it.message, it)
            }.collectLatest {
                logOutLiveData.value = Resource.Success(it)
            }
        }
    }
}